const NewsModel = require('../../models/NewsModel')

const NewsService = {
  add: async ({ title, content, category, isPublish, cover, editTime }) => {
    // 数据库的模型处理
    return NewsModel.create({
      title,
      content,
      category,
      isPublish,
      cover,
      editTime
    })
  },

  getList: async ({ _id }) => {
    return _id ? NewsModel.find({ _id }) : NewsModel.find({})
  },
  updateList: async ({ _id, title, content, category, isPublish, cover, editTime }) => {
    if (cover) {
      return NewsModel.updateOne({ _id }, { title, content, category, isPublish, cover, editTime })
    } else {
      return NewsModel.updateOne({ _id }, { title, content, category, isPublish, editTime })
    }
  },
  delList: async ({ _id }) => {
    return NewsModel.deleteOne({ _id })
  },
  publish: async ({ _id, isPublish, editTime }) => {
    return NewsModel.updateOne(
      {
        _id
      },
      {
        isPublish,
        editTime
      }
    )
  }
}

module.exports = NewsService
